package com.cyx.service.impl;

import com.cyx.mapper.StudentMapper;
import com.cyx.pojo.PageResult;
import com.cyx.pojo.Student;
import com.cyx.pojo.StudentQueryParam;
import com.cyx.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageResult<Student> page(StudentQueryParam studentQueryParam) {
        // 1、设置分页参数
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        // 2、执行查询
        List<Student> studentList = studentMapper.list(studentQueryParam);
        // 3、解析查询结果 并封装
        Page<Student> stu = (Page<Student>) studentList;
        return new PageResult<>(stu.getTotal(),stu.getResult());
    }

    @Override
    public void addStudent(Student student) {
        // 更新时间
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insertStudent(student);
    }

    @Override
    public Student getInfoById(Integer id) {
        return studentMapper.getStudentInfoById(id);
    }

    @Override
    public void updateStudent(Student student) {
        // 更新相关属性
        student.setUpdateTime(LocalDateTime.now());
        // 调用Mapper接口方法
        studentMapper.updateStudent(student);
    }

    @Override
    public void deleteStudentByIds(List<Integer> ids) {
        studentMapper.deleteStudentByIds(ids);
    }

    @Override
    public void violationStudent(Integer id, Short score) {
        studentMapper.violationStudent(id, score);
    }
}
